// 主要JavaScript功能
document.addEventListener('DOMContentLoaded', function() {
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 参观者感言轮播
    const testimonialItems = document.querySelectorAll('.testimonial-item');
    const testimonialDots = document.querySelectorAll('.testimonial-dots .dot');
    
    if (testimonialItems.length > 0 && testimonialDots.length > 0) {
        let currentTestimonial = 0;
        
        // 显示指定感言
        function showTestimonial(index) {
            testimonialItems.forEach(item => {
                item.style.display = 'none';
            });
            
            testimonialDots.forEach(dot => {
                dot.classList.remove('active');
            });
            
            testimonialItems[index].style.display = 'block';
            testimonialDots[index].classList.add('active');
            
            currentTestimonial = index;
        }
        
        // 初始化显示第一个
        showTestimonial(0);
        
        // 点击切换
        testimonialDots.forEach((dot, index) => {
            dot.addEventListener('click', () => {
                showTestimonial(index);
            });
        });
        
        // 自动切换
        setInterval(() => {
            let next = currentTestimonial + 1;
            if (next >= testimonialItems.length) {
                next = 0;
            }
            showTestimonial(next);
        }, 6000);
    }
    
    // 动画效果
    const animateElements = document.querySelectorAll('.animate-on-scroll');
    
    function checkScroll() {
        animateElements.forEach(element => {
            const elementTop = element.getBoundingClientRect().top;
            const windowHeight = window.innerHeight;
            
            if (elementTop < windowHeight * 0.8) {
                element.classList.add('animate');
            }
        });
    }
    
    // 初始检查
    checkScroll();
    
    // 滚动时检查
    window.addEventListener('scroll', checkScroll);
});

// 作品筛选功能
document.addEventListener('DOMContentLoaded', function() {
    // 分类筛选
    const categoryButtons = document.querySelectorAll('[data-filter]');
    const techniqueButtons = document.querySelectorAll('[data-technique]');
    const galleryItems = document.querySelectorAll('.gallery-item');
    
    // 分类按钮点击事件
    categoryButtons.forEach(button => {
        button.addEventListener('click', function() {
            // 移除所有按钮的活动状态
            categoryButtons.forEach(btn => btn.classList.remove('active'));
            // 添加当前按钮的活动状态
            this.classList.add('active');
            
            filterGallery();
        });
    });
    
    // 针法按钮点击事件
    techniqueButtons.forEach(button => {
        button.addEventListener('click', function() {
            // 移除所有按钮的活动状态
            techniqueButtons.forEach(btn => btn.classList.remove('active'));
            // 添加当前按钮的活动状态
            this.classList.add('active');
            
            filterGallery();
        });
    });
    
    // 搜索功能
    const searchInput = document.getElementById('gallery-search');
    const searchBtn = document.getElementById('search-btn');
    
    searchBtn.addEventListener('click', function() {
        filterGallery();
    });
    
    searchInput.addEventListener('keyup', function(e) {
        if (e.key === 'Enter') {
            filterGallery();
        }
    });
    
    // 筛选画廊函数
    function filterGallery() {
        const activeCategory = document.querySelector('[data-filter].active').getAttribute('data-filter');
        const activeTechnique = document.querySelector('[data-technique].active').getAttribute('data-technique');
        const searchTerm = searchInput.value.toLowerCase();
        
        galleryItems.forEach(item => {
            const categories = item.getAttribute('data-category').split(' ');
            const technique = item.getAttribute('data-technique');
            const title = item.querySelector('h3').textContent.toLowerCase();
            const author = item.querySelector('.overlay-content p').textContent.toLowerCase();
            
            // 检查是否符合所有筛选条件
            const matchCategory = activeCategory === 'all' || categories.includes(activeCategory);
            const matchTechnique = activeTechnique === 'all' || technique === activeTechnique;
            const matchSearch = searchTerm === '' || title.includes(searchTerm) || author.includes(searchTerm);
            
            if (matchCategory && matchTechnique && matchSearch) {
                item.style.display = 'block';
            } else {
                item.style.display = 'none';
            }
        });
    }
    
    // 加载更多功能
    const loadMoreBtn = document.getElementById('load-more-btn');
    
    loadMoreBtn.addEventListener('click', function() {
        // 这里可以添加加载更多作品的逻辑
        alert('加载更多作品功能将在后续版本中实现');
    });
    
    // 移除弹窗相关功能
    // 移除查看详情按钮点击事件和弹窗显示逻辑
});